package com.ccx.library.Handler.AOP;

import com.ccx.library.Utils.HttpUtils;
import com.ccx.library.Utils.TokenUtils;
import com.ccx.library.Vo.SysLog;
import com.ccx.library.annotation.Log;
import lombok.extern.slf4j.Slf4j;
import org.aspectj.lang.JoinPoint;
import org.aspectj.lang.annotation.Aspect;
import org.aspectj.lang.annotation.Before;
import org.aspectj.lang.annotation.Pointcut;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;

import javax.servlet.http.HttpServletRequest;
import java.util.UUID;

@Aspect
@Component
@Slf4j
public class LogHandler {


    @Pointcut("@annotation(com.ccx.library.annotation.Log)")
    public void Log(){}

    /**
     * 操作
     * @param joinPoint
     * @return
     * @throws Throwable
     */
    @Before("@annotation(Log)")
    public void doAroundLog(JoinPoint joinPoint, Log Log) throws Throwable {
        // 内容
        String LogInfo = Log.LogInfo();
        // 操作类型
        String type = Log.LogType().getType();
        // 操作模块
        String module = Log.LogModule().getType();
        HttpServletRequest request = HttpUtils.getRequest();
        SysLog sysLog = new SysLog();
        sysLog.setLogId(UUID.randomUUID().toString());
        sysLog.setLogInfo(LogInfo);
        sysLog.setLogModule(module);
        sysLog.setRequestUrl(request.getRequestURI());
        sysLog.setLogIpAddr(HttpUtils.getIpAddress(request));
        sysLog.setLogType(type);

        log.info("日志输出==>"+sysLog);
    }

}
